import {
	getFileExtension,
	uuid
} from '@/utils/tools';
import moment from "moment"

export const routerTo = (url, type = 'navigate') => {
	if (type == 'navigate') {
		uni.navigateTo({
			url
		})
	} else if (type == 'redirect') {
		uni.redirectTo({
			url
		})
	} else if (type == 'reLaunch') {
		uni.reLaunch({
			url
		})
	} else if (type == 'switchTab') {
		uni.switchTab({
			url
		})
	} else {
		uni.showToast({
			title: "信息错误",
			icon: "none"
		})
	}
}

export const goBack = (delta = 1) => {
	uni.navigateBack({
		delta
	})
}

// 上传图片
export const uploadPictures = (count = 1, callback) => {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count,
			success: res => {
				if (res.tempFilePaths.length > 0) {
					uni.showLoading({
						title: "上传中",
						mask: true
					})
					let filePath = res.tempFilePaths[0];
					let suffix = getFileExtension(res.tempFiles[0].name);
					uniCloud.uploadFile({
						filePath,
						cloudPath: `raffle/${moment().format('YYYYMMDD')}/${uuid()}.${suffix}`,
						cloudPathAsRealPath: true,
						onUploadProgress: function(progressEvent) {
							var progress = Math.round(
								(progressEvent.loaded * 100) / progressEvent.total
							);
							callback(progress)
						},
						success: res => {
							uni.hideLoading();
							console.log("文件上传成功---------->", res);
							resolve(res.fileID);
						},
						fail: err => {
							uni.hideLoading();
							reject(err);
						}
					})
				}
			}
		})
	})
}

// 根据活动状态码转换成文字
export function activeState(state) {
	if (state === 1) {
		return "未开始";
	} else if (state == 2) {
		return "进行中";
	} else if (state == 3) {
		return "已结束";
	} else {
		return "已结束";
	}
}